/* ***************************************************** */
/* Arcans Team (2010), website                           */
/* Site under GNU GPL v3 Licence                         */
/* (see NOTICE and COPYING in the of the site file tree) */
/* ***************************************************** */

/* ***************************************************** */
/* fonction initFun, lancement des scripts               */
/* ***************************************************** */
var initFun = function () { voile.init(); initSession(); scrollFun(); }
/* ***************************************************** */
/* Singleton AJAX                                        */
/* ***************************************************** */
var ajax=new XMLHttpRequest();
var ajaxFun=function(add,tabp,fres){
	ajax.onreadystatechange=function(){if(ajax.readyState==4){if(fres!="")fres(ajax.responseXML);}}
	ajax.open("POST",absolute_path+"?ajax="+add,true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); var d="";
	for(var i=0;i<tabp.length;i++){if(i!=0)d=d+"&";d=d+"info"+i+"="+tabp[i];}
	ajax.send(d);
}
/* ***************************************************** */
/* Réécriture des document.getElement                    */
/* ***************************************************** */
var doc = document;
Document.prototype.byID = function (ch) { return this.getElementById(ch); }
Node.prototype.byCLASS = function (ch) { return this.getElementsByClassName(ch); }
Node.prototype.byTAG = function (ch) { return this.getElementsByTagName(ch); }
/* ***************************************************** */
/* variables globales, pour la capture de la souris      */
/* ***************************************************** */
var souris_capt=0; var scroll_capt=new Array();
/* ***************************************************** */
/* fonction addNodeFun, création simplifiée de node      */
/* ***************************************************** */
function addNodeFun (type_elt,attr_tab,parent_elt) {
	var nodec = doc.createElementNS("http://www.w3.org/1999/xhtml",type_elt); var reg_exp = /^on/;
	if (attr_tab != "") {
		for (attr in attr_tab) {
			if (attr == "texte") nodec.appendChild(doc.createTextNode(attr_tab[attr]));
			else if (reg_exp.test(attr)) nodec.setAttribute(attr,"javascript:"+attr_tab[attr]);
			else nodec.setAttribute(attr,attr_tab[attr]);
		}
	}
	parent_elt.appendChild(nodec); return nodec;
}
/* ***************************************************** */
/* fonction modNodeFun, modification de node             */
/* ***************************************************** */
function modNodeFun (nodec,attr_tab) {
	var reg_exp = /^on/;
	for (attr in attr_tab) {
		if (attr == "texte") {
			if (nodec.hasChildNodes()) nodec.replaceChild(doc.createTextNode(attr_tab[attr]),nodec.firstChild);
			else nodec.appendChild(doc.createTextNode(attr_tab[attr]));
		}
		else if (reg_exp.test(attr)) nodec.setAttribute(attr,"javascript:"+attr_tab[attr]);
		else nodec.setAttribute(attr,attr_tab[attr]);
	}
}
/* ***************************************************** */
/* fonction supNodeFun, suppression de node              */
/* ***************************************************** */
function supNodeFun (nodec) { nodec.parentNode.removeChild(nodec); }
/* ***************************************************** */
/* fonction scrollFun, mise en place du scroll           */
/* ***************************************************** */
function cloneNodeFun (node_old,node_new) {
	var node_lst = node_old.childNodes;
	for (var iter=0;iter<node_lst.length;iter++) {
		if (node_lst[iter].nodeType == 1) {
			nodec = addNodeFun(node_lst[iter].nodeName,"",node_new);
			if (node_lst[iter].attributes.length != 0) {
				var attr_tab = node_lst[iter].attributes;
				for (var it=0;it<attr_tab.length;it++) nodec.setAttribute(attr_tab[it].nodeName,attr_tab[it].nodeValue);
			}
			if (node_lst[iter].hasChildNodes()) cloneNodeFun(node_lst[iter],nodec);
		}
		else if (node_lst[iter].nodeType == 3) node_new.appendChild(node_lst[iter].cloneNode(true));
	}
}
/* ***************************************************** */
/* fonction scrollFun, mise en place du scroll           */
/* ***************************************************** */
function scrollFun () {
	var wraps_cont = document.getElementsByClassName("wrapper");
	for (var iter=0;iter<wraps_cont.length;iter++) {
		var cur_w = wraps_cont[iter];
		var cur_height = doc.defaultView.getComputedStyle(cur_w,null).height;
		cur_height = parseInt(cur_height.slice(0,cur_height.length-2));
		if (cur_height+30 < 500) cur_w.parentNode.style.height = cur_height+"px";
		else if (cur_height+30 > 500) {
			var scroll_bar = addNodeFun("div",{"class":"scroll_bar","style":"top:-"+cur_height+"px;"},cur_w.parentNode);
			var btn_height = Math.round(500*(500/cur_height)); if (btn_height <10) btn_height=10;
			var id_div="btn_scll_"+wraps_cont[iter].getAttribute("id");
			var wrap_block = wraps_cont[iter].getElementsByClassName("wrap_block")[0];
			wrap_block.style.top="0px";
			while (wraps_cont[iter].childNodes.length > 1) wrap_block.appendChild(wraps_cont[iter].childNodes.item(0));
			var evt="scrollMouvFun('"+wraps_cont[iter].getAttribute("id")+"','"+id_div+"',"+scroll_bar.offsetTop+");";
			style = "height:"+btn_height+"px;top:"+scroll_bar.offsetTop+"px;";
			addNodeFun("div",{"class":"scroll_btn","style":style,"id":id_div,"onmousedown":evt},cur_w.parentNode);
		}
	}
}
/* ***************************************************** */
/* fonction scrollMouvFun, capt d'event pour le scroll   */
/* ***************************************************** */
function scrollMouvFun (id_sec,id_btn,sb_top) {
	souris_capt=window.event.clientY;
	doc.onmousemove = function test_move () {
		var diff_Y = window.event.clientY-souris_capt; var btn_curs = doc.byID(id_btn).style;
		var btn_curT_v = parseInt(btn_curs.top.substring(0,btn_curs.top.length-2));
		var wrap_block = doc.byID(id_sec).firstChild;
		if ((btn_curT_v+diff_Y) < sb_top) { btn_curs.top=sb_top+"px"; wrap_block.style.top="0px"; }
		else if ((btn_curT_v+diff_Y+parseInt(btn_curs.height.substring(0,btn_curs.height.length-2))) > (sb_top+500)) {}
		else { doc.byID(id_btn).style.top=(btn_curT_v+diff_Y)+"px";
			var wrap_height = doc.defaultView.getComputedStyle(wrap_block,null).height;
			wrap_height = parseInt(wrap_height.substring(0,wrap_height.length-2));
			var pct=(btn_curT_v-sb_top)/(500 - parseInt(btn_curs.height.substring(0,btn_curs.height.length-2)));
			wrap_block.style.top="-"+((wrap_height-495)*pct)+"px";
		}
		souris_capt=window.event.clientY;
	};
	doc.onmouseup = function () { doc.onmousemove=null; souris_capt=0;  };
}